<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This HtmlOutputter exactly output the php content to the visitor.
 * Currently eliminates the handlers.
 * 
 * 直接往浏览器输出HTML标记语言的包装类.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 1) Add some HTML elements.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/lib/slh_xmloutputter.php';

/**
 * Low-level generator for HTML.
 *
 * Abstracts some of the code necessary for HTML generation. Especially
 * has methods for generating HTML form elements. Note that these have
 * been created kind of haphazardly, not with an eye to making a general
 * HTML-creation class.
 */
class HTMLOutputter extends XMLOutputter
{
	/**
     * Constructor.
     *
     * Just wraps the XMLOutputter constructor.
     *
     * @param string  $output 		URI to output to, default = stdout
     * @param boolean $indent 		whether to indent output, default true
     */

    function __construct($output = 'php://output', 
    					 $indent = null)
    {
        parent::__construct($output, $indent);
    }
    
    /**
     * Start an HTML document.
     * 
     * Currently ignores the content negotiation...
     */
    function startHTML($type = null)
    {
// FIXME: Content and language negotiation.
/*
    	if (!$type) {
            $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
              $_SERVER['HTTP_ACCEPT'] : null;

            // XXX: allow content negotiation for RDF, RSS, or XRDS

            $cp = common_accept_to_prefs($httpaccept);
            $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);

            $type = common_negotiate_type($cp, $sp);

            if (!$type) {
                // TRANS: Client exception 406
                throw new ClientException(_('This page is not available in a '.
                                            'media type you accept'), 406);
            }
        }
*/
    	// Default type and language.
    	$type = 'text/html';
    	$language = 'zh-CN';
    	
    	header('Content-Type: ' . $type);
    	
    	$this->extraHead();

// TODO: For xml document.
/*
     	if (preg_match("/.*\/.*xml/", $type)) {
            // Required for XML documents
            $this->xw->startDocument('1.0', 'UTF-8');
        }
*/
    	
    	// Strict type dtd.
    	$this->xw->writeDTD('html',
    						'-//W3C//DTD XHTML 1.0 Strict//EN',
                            'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
    	$attrs = array(
            'xmlns' => 'http://www.w3.org/1999/xhtml',
            'xml:lang' => $language,
            'lang' => $language
        );
        
        $this->elementStart('html', $attrs);
    }
    
    /**
     * Ends an HTML document.
     */
    function endHTML()
    {
    	$this->elementEnd('html');
    	$this->endXML();
    }
    
    /**
     * To specify additional HTTP headers for the action.
     */
    function extraHead()
    {
    	// Needs to be overloaded.
    }
    
    /**
     * Output a HTML text input element.
     * 
     * Despite the name, it is specifically for outputting a text
     * input element, not other <input> elements. It outputs a 
     * cluster of elements, including a <label> and an associated
     * instructions span.
     */
	function input($id, 
				   $label, 
				   $value = null, 
				   $instructions=null)
    {
        $this->element('label', array('for' => $id), $label);
        $attrs = array('name' => $id,
                       'type' => 'text',
                       'id' => $id);
        if ($value) {
            $attrs['value'] = $value;
        }
        
        $this->element('input', $attrs);
        
        if ($instructions) {
// FIXME: What is a form_guide?
            $this->element('p', 'form_guide', $instructions);
        }
    }
    
    /**
     * TODO: More css style?
     * 
     * Output a HTML sumbit button.
     */
	function submit($id, 
					$label, 
					$cls='submit', 
					$name=null, 
					$title=null)
    {
        $this->element('input', array('type' => 'submit',
                                      'id' => $id,
                                      'name' => ($name) ? $name : $id,
                                      'class' => $cls,
                                      'value' => $label,
                                      'title' => $title));
    }
}

// FIXME: To add other output elements.!
// Just for testing now.

?>